package com.gla.market.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.gla.market.R;
import com.gla.market.entity.FYH_Evaluation;
import com.gla.market.entity.FYH_Vedio;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.util.List;

public class FYH_VedioAdapter extends BaseAdapter {
    private Context context;
    private List<FYH_Vedio> listvdo;

    public FYH_VedioAdapter(Context context, List<FYH_Vedio> listvdo) {
        this.context = context;
        this.listvdo = listvdo;
        //实例化ImageLoader
        ImageLoaderConfiguration configuration = ImageLoaderConfiguration.createDefault(context);
        ImageLoader.getInstance().init(configuration);
    }

    @Override
    public int getCount() {
        return listvdo.size();
    }

    @Override
    public Object getItem(int i) {
        return listvdo.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        FYH_VedioHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.fyh_vedio_card, null);

            //布局文件中所有组件的对象封装到ViewHolder对象中
            holder = new FYH_VedioHolder();
            holder.vname = convertView.findViewById(R.id.vedio_name);
            holder.vphoto = convertView.findViewById(R.id.vedio_photo);
            holder.vid = convertView.findViewById(R.id.vid);
            //把ViewHolder对象封装到View对象中
            convertView.setTag(holder);
        } else { holder = (FYH_VedioHolder) convertView.getTag(); }

        //获取点击的子菜单的View
        FYH_Vedio ev = listvdo.get(position);
        String name = ev.getName();
        if(name.length() > 15) name =  name.substring(0,15) + "...";
        String photo = ev.getCover_url();

        holder.vname.setText(name);
        holder.vid.setText(ev.getId() + "");
        //绑定图片到控件
        if(!"".equals(photo)) {
            ImageLoader.getInstance().displayImage(photo, holder.vphoto);
        }else{
            holder.vphoto.setImageResource(R.drawable.fyh_pet_mr);
        }
        return convertView;
    }
}
